package com.electric.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.electric.common.LoggerInterface;
import com.electric.service.IRepairRecordStatisticsService;
import com.electric.service.IRepairTypeService;
import com.electric.service.IVoltageLevelService;
import com.google.gson.Gson;

/**
 * <p>
 * 故障记录统计 前端控制器
 * </p>
 *
 * @author zhangbin
 * @since 2018-08-28
 */
@Controller
@RequestMapping("/statistics")
public class RepairRecordStatisticsController implements LoggerInterface {
	@Autowired
	private IRepairRecordStatisticsService statisticsService;
	@Autowired
	private IRepairTypeService repairTypeService;
	@Autowired
	private IVoltageLevelService voltageLevelService;
	

	@RequestMapping("/init")
	public ModelAndView init(@RequestParam(required = false) String statisticsType,
			@RequestParam(required = false) String startDate, @RequestParam(required = false) String endDate) {
		ModelAndView view = new ModelAndView("/homePage");
		
		List<Map<String,Object>> customerPie = this.statisticsCustomerPie(startDate, endDate);
		List<Map<String,Object>> repairTypePie = this.statisticsRepairTypePie(startDate, endDate);
		List<Map<String,Object>> voltageLevelPie = this.statisticsVoltageLevelPie(startDate, endDate);
		List<Map<String,Object>> repairTypeLine = this.statisticsRepairTypeLine(statisticsType, startDate, endDate);
		List<Map<String,Object>> voltageLevelLine = this.statisticsVoltageLevelLine(statisticsType, startDate, endDate);
		List<Map<String,Object>> customerVoltageLoss = this.statisticsCustomerVoltageLoss(startDate, endDate);
		
		view.addObject("customerPie",new Gson().toJson(customerPie));
		view.addObject("repairTypePie",new Gson().toJson(repairTypePie));
		view.addObject("voltageLevelPie",new Gson().toJson(voltageLevelPie));
		view.addObject("repairTypeLine",new Gson().toJson(repairTypeLine));
		view.addObject("allRepairType",new Gson().toJson(repairTypeService.selectAllRepairType()));
		view.addObject("voltageLevelLine",new Gson().toJson(voltageLevelLine));
		view.addObject("allVoltageLevel",new Gson().toJson(voltageLevelService.selectAllVoltageLevel()));
		view.addObject("customerVoltageLoss",new Gson().toJson(customerVoltageLoss));

		view.addObject("statisticsType",statisticsType);
		view.addObject("startDate",startDate);
		view.addObject("endDate",endDate);
		return view;
	}

	/**
	 * 根据用户和时间段统计饼图
	 * 
	 * @param statisticsType
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@RequestMapping("statisticsCustomerPie")
	@ResponseBody
	public List<Map<String, Object>> statisticsCustomerPie(String startDate, String endDate) {
		return statisticsService.statisticsCustomerPie(startDate, endDate);
	}

	/**
	 * 根据电压等级和时间段统计饼图
	 * 
	 * @param statisticsType
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@RequestMapping("statisticsVoltageLevelPie")
	@ResponseBody
	public List<Map<String, Object>> statisticsVoltageLevelPie(String startDate, String endDate) {
		return statisticsService.statisticsVoltageLevelPie(startDate, endDate);
	}

	/**
	 * 根据维修类型和时间段统计饼图
	 * 
	 * @param statisticsType
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@RequestMapping("statisticsRepairTypePie")
	@ResponseBody
	public List<Map<String, Object>> statisticsRepairTypePie(String startDate, String endDate) {
		return statisticsService.statisticsRepairTypePie(startDate, endDate);
	}

	/**
	 * 根据电压等级和时间段统计折线图
	 * 
	 * @param statisticsType
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@RequestMapping("statisticsVoltageLevelLine")
	@ResponseBody
	public List<Map<String, Object>> statisticsVoltageLevelLine(String statisticsType, String startDate,
			String endDate) {
		return statisticsService.statisticsVoltageLevelLine(statisticsType, startDate, endDate);
	}

	/**
	 * 根据维修类型和时间段统计折线图
	 * 
	 * @param statisticsType
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@RequestMapping("statisticsRepairTypeLine")
	@ResponseBody
	public List<Map<String, Object>> statisticsRepairTypeLine(String statisticsType, String startDate, String endDate) {
		return statisticsService.statisticsRepairTypeLine(statisticsType, startDate, endDate);
	}
	
	/**
	 * 根据电压等级用户统计失压情况
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@RequestMapping("statisticsCustomerVoltageLoss")
	@ResponseBody
	public List<Map<String, Object>> statisticsCustomerVoltageLoss(String startDate, String endDate) {
		return statisticsService.statisticsCustomerVoltageLoss(startDate, endDate);
	}
}
